[% setvar title Elements of @_ should be read-only by default %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Elements of @_ should be read-only by default</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: John Tobey &lt;<a href='mailto:jtobey@john-edwin-tobey.org'>jtobey@john-edwin-tobey.org</a>&gt;
  Date: 28 Sep 2000
  Last Modified: 1 Oct 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 344
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Unprototyped subs should not be allowed to modify their callers' data
simply by assigning to elements of the arg array.</p>
<a name='COMMENTS ON FREEZE'></a><h1>COMMENTS ON FREEZE</h1>
<p>This RFC generated no discussion in 3 days.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>In Perl 5, you can modify a caller's value by assigning directly to
elements of <code>@_</code>, like this:</p>
<pre>    sub perp {
        $_[0] = 'ha!';
    }

    sub victim {
        my $num = 42;
        perp($num);
        print $num;    # prints ha!
    }</pre>
<p>This form of passing arguments by reference is obsolete now that Perl
has hard references and <code>\$</code> in prototypes.</p>
<p>The feature is surprising.  At least, I was surprised when I first
learned you could modify a caller's value by assigning to $_[0].</p>
<p>The feature is confusing, since it means that the recommended
convention for naming parameters (by assigning <code>@_</code> to a <code>my</code> list)
alters semantics.  For example, the following subs do <i>not</i> have the
same effect as above:</p>
<pre>    sub perp1 {
        my $arg = shift;
        $arg = 'ha!';
    }

    sub perp2 {
        my ($arg) = @_;
        $arg = 'ha!';
    }</pre>
<p>The Perl 5 (and older) behavior may preclude optimizations in compiled
code.  If a compiler knows that arguments are passed by value, it may
generate code that places the value directly in a register or on the
stack.  With the Perl 5 semantics, it would normally have to pass a
pointer to each scalar and dereference the pointer to obtain the
argument's value.</p>
<p>I think this change should not affect subs with a prototype, so the
examples in <i><a href='http://search.cpan.org/perldoc?perlsub#Prototypes'>&quot;Prototypes&quot; in perlsub</a></i> would still work.  People who bother
to use prototypes and sub attributes should know what they are getting
into.  The prototype and attribute system will, I think, give plenty
of opportunities to specify compiler optimizations.  Just the default
case should be changed.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>A fascist implementation would emit a compile-time error any time
<code>@_</code> or one of its elements were assigned to, taken a refernce to,
etc.  A friendlier version would automatically copy the arg value to a
new temporary.</p>
<p>This change would mean a moderate Perl 5 compatibility breakage.  The
Perl-5-to-Perl-6 converter could insert whatever trick is used to
obtain the Perl 5 behavior (perhaps a <code>(@)</code> prototype) when it
detects (or suspects) argument modification.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 154: Simple assignment lvalue subs should be on by default</p>
<p><i><a href='http://search.cpan.org/perldoc?perlsub'>perlsub</a></i></p>
<p>The C-- homepage - <a href='http://www.cminusminus.org/' target='_blank'>www.cminusminus.org</a></p>
</div>
